package com.stylefeng.guns.modular.system.task;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.HttpsClient;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.GpcMapper;
import com.stylefeng.guns.modular.system.dao.NbGpcWtMapper;
import com.stylefeng.guns.modular.system.dao.YdayGpcMapper;
import com.stylefeng.guns.modular.system.model.NbGpcWt;

@Service
public class GpcWtImportTask {

	@Autowired
	private NbGpcWtMapper gpcWtMapper;
	
	@Autowired
	private YdayGpcMapper ydayGpcMapper;
	
	@Autowired
	private GpcMapper gpcMapper;

	public void execute(boolean isHoliday) {
		if(isHoliday){
			Date now = new Date();
			int hours = now.getHours();
			int minutes = now.getMinutes();
			boolean isRun = false;
			if(hours > 9){
				isRun = true;
			}else{
				if(hours == 9 && minutes > 15){
					isRun = true;
				}
			}
			if(isRun){
				List<String> gpcs = gpcMapper.selectCodeListByDate(DateUtil.getAfterDayDate(new Date(),"-1"));
				if(CollectionUtils.isEmpty(gpcs)){
					gpcs = gpcMapper.selectCodeListByDate(DateUtil.getAfterDayDate(new Date(),"-3"));
				}
				for(String code : gpcs){
					if(code.startsWith("6")){
						continue;
					}
					String result = HttpsClient.postSSL(ImportContants.getWtPathStr(code,1), null,true);
					JSONArray jsonArray =  JSON.parseObject(result).getJSONArray("List");
					for(int i=0;i<jsonArray.size();i++){
						String v = StringUtils.remove(jsonArray.get(i).toString(), "\"");
						String[] valus = StringUtils.split(StringUtils.substringBetween(v, "[", "]"),",");
						NbGpcWt gpcWt = new NbGpcWt();
						gpcWt.setgScode(code);
						gpcWt.setgNum(Double.valueOf(valus[4]));
						Date nowTime = DateUtil.parseTime(DateUtil.getDay().concat(" ").concat(valus[0]).concat(":00"));
						gpcWt.setgTime(nowTime);
						if(nowTime.getTime() < DateUtil.getBeforeDate("-5").getTime()){
							continue;
						}
						gpcWt = gpcWtMapper.selectByTime(gpcWt);
						if(gpcWt == null){
							gpcWt = new NbGpcWt();
							gpcWt.setgScode(code);
							gpcWt.setgTime(nowTime);
							gpcWt.setgPrice(Double.valueOf(valus[2]));
							gpcWt.setgNum(Double.valueOf(valus[4]));
							gpcWt.setgBType(Long.valueOf(valus[5]));
							gpcWt.setgIsC(Long.valueOf(valus[8]));
							gpcWt.setgIsB(Long.valueOf(valus[7]));
							gpcWtMapper.insert(gpcWt);
						}else{
							gpcWt.setgPrice(Double.valueOf(valus[2]));
							gpcWt.setgNum(Double.valueOf(valus[4]));
							gpcWt.setgBType(Long.valueOf(valus[5]));
							gpcWt.setgIsC(Long.valueOf(valus[8]));
							gpcWt.setgIsB(Long.valueOf(valus[7]));
							gpcWtMapper.updateById(gpcWt);
						}
					}
				}

				List<String> ydayList = ydayGpcMapper.selectCodeListByDate(new Date());
				for(String code : ydayList){
					if(code.startsWith("6")){
						continue;
					}
					String result = HttpsClient.postSSL(ImportContants.getWtPathStr(code,1), null,true);
					JSONArray jsonArray =  JSON.parseObject(result).getJSONArray("List");
					for(int i=0;i<jsonArray.size();i++){
						String v = StringUtils.remove(jsonArray.get(i).toString(), "\"");
						String[] valus = StringUtils.split(StringUtils.substringBetween(v, "[", "]"),",");
						NbGpcWt gpcWt = new NbGpcWt();
						gpcWt.setgScode(code);
						gpcWt.setgNum(Double.valueOf(valus[4]));
						Date nowTime = DateUtil.parseTime(DateUtil.getDay().concat(" ").concat(valus[0]).concat(":00"));
						gpcWt.setgTime(nowTime);
						if(nowTime.getTime() < DateUtil.getBeforeDate("-30").getTime()){
							continue;
						}
						gpcWt = gpcWtMapper.selectByTime(gpcWt);
						if(gpcWt == null){
							gpcWt = new NbGpcWt();
							gpcWt.setgScode(code);
							gpcWt.setgTime(nowTime);
							gpcWt.setgPrice(Double.valueOf(valus[2]));
							gpcWt.setgNum(Double.valueOf(valus[4]));
							gpcWt.setgBType(Long.valueOf(valus[5]));
							gpcWt.setgIsC(Long.valueOf(valus[8]));
							gpcWt.setgIsB(Long.valueOf(valus[7]));
							gpcWtMapper.insert(gpcWt);
						}else{
							gpcWt.setgPrice(Double.valueOf(valus[2]));
							gpcWt.setgNum(Double.valueOf(valus[4]));
							gpcWt.setgBType(Long.valueOf(valus[5]));
							gpcWt.setgIsC(Long.valueOf(valus[8]));
							gpcWt.setgIsB(Long.valueOf(valus[7]));
							gpcWtMapper.updateById(gpcWt);
						}
					}
				}
			}
		}
	}

	public static void main(String[] args) {
		Date now = new Date();
		int hours = now.getHours();
		int minutes = now.getMinutes();
		System.out.println(hours);
		System.out.println(minutes);
	}
}
